import API from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headimgurl:'',
    nickname:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.nickname) {
      wx.setStorageSync('nickname',options.nickname)
      this.setData({
        nickname: options.nickname,
        headimgurl: options.headimgurl
      })
    }

  },
  tofileUp () {
    wx.navigateTo({
      url: '/pages/cutInside/cutInside?type=2'
    })
  },
  toMyintrducePage () {
    wx.navigateTo({
      url: '/pages/myIntrduce/myIntrduce',
    })
  },
  changeNickName (e) {
    wx.setStorageSync('nickname',e.detail.value),
    this.setData({
      nickname: e.detail.value
    })
  },
  /*-- 图片上传 --*/
  fileUp() {
    var that = this
    wx.uploadFile({
      url: 'https://lequer.odeman.com/weixin/Decaler/upload_pic',
      filePath: wx.getStorageSync('UpHeadPic'),
      name: 'file',
      formData: {
        pwd: 9999
      },
      success(res) {
        var data = JSON.parse(res.data)
        console.log(data.url)
        if (data.code == 0) {
          that.setData({
            headimgurl: data.url
          })
        }

      }
    })
  },
  updateInfomation () {
    var data = {
      headimgurl:this.data.headimgurl,
      introduce: wx.getStorageSync('myIntrduce'),
      nickname: wx.getStorageSync('nickname')
    }
    API.updateInfomation(data).then(res=>{
      if (res.code == 0) {
        wx.showToast({
          title: '更新成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(res=>{
          wx.redirectTo({
            url: '/pages/myMainPage/myMainPage'
          })
        },2000)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('UpHeadPic')) {
      this.fileUp()
    }
    console.log(wx.getStorageSync('nickname'))
    if (wx.getStorageSync('nickname')) {
      this.setData({
        nickname:wx.getStorageSync('nickname')
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})